import Vue from 'vue'
import Router from 'vue-router'
import Error404 from '@/pages/common/404'
import Articles from '@/pages/articles/index'

const originalPush = Router.prototype.push
Router.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const _import = (file) => require(`@/pages/${file}.vue`).default

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'articles',
      component: Articles,
      children: [
        { path: '/dividing-ruler', name: 'dividingRuler', component: _import('articles/dividing-ruler'), meta: {title: '刻度尺样式'} }
      ]
    },
    { path: '*', name: 'error404', component: Error404, meta: {title: '404'} }
  ]
})

router.afterEach((to, from) => {
  document.title = to.meta.title ? `${to.meta.title} - WEBAPP` : 'WEBAPP'
})

export default router
